package com.cuteboy.realm;

import com.cuteboy.dao.*;
import com.cuteboy.entity.SysOperationLog;
import com.cuteboy.entity.SysPermission;
import com.cuteboy.entity.SysRole;
import com.cuteboy.entity.SysUser;
import com.cuteboy.util.ValidateUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysPermissionMapper sysPermissionMapper;

    /**
     * 授权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String username = principalCollection.getPrimaryPrincipal().toString();

        //User user=userRepository.findByUserName(userName);
        //根据用户名查询出用户记录
        Example sysUserExample = new Example(SysUser.class);
        sysUserExample.or().andEqualTo("username",username);
        SysUser sysUser = sysUserMapper.selectByExample(sysUserExample).get(0);


        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        //List<Role> roleList=roleRepository.findByUserId(user.getId());
        List<SysRole> roleList = sysRoleMapper.selectRolesByUsername(sysUser.getUsername());

        Set<String> roles=new HashSet<String>();
        if(roleList.size()>0){
            for(SysRole role:roleList){
                roles.add(role.getName());
                //List<Tmenu> menuList=menuRepository.findByRoleId(role.getId());
                //根据角色id查询所有资源
                List<SysPermission> permissionList=sysPermissionMapper.selectPermissionByRoleId(role.getId());
                for(SysPermission permission:permissionList){
                    if (ValidateUtils.isNotEmpty(permission.getFlag())){
                        info.addStringPermission(permission.getFlag()); // 添加权限
                    }
                }
            }
        }
        info.setRoles(roles);
        return info;
    }

    /**
     * 权限认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = authenticationToken.getPrincipal().toString();
        //User user=userRepository.findByUserName(userName);
        Example tuserExample = new Example(SysUser.class);
        tuserExample.or().andEqualTo("username",username);
        SysUser sysUser = sysUserMapper.selectByExample(tuserExample).get(0);
        if(sysUser!=null){
            AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(sysUser.getUsername(),sysUser.getPassword(),"xxx");
            return authcInfo;
        }else{
            return null;
        }
    }
}
